﻿/// <summary>
/// Copyright(c) 2008-2009北京玖富时代投资顾问有限公司, All Rights Reserved.
/// 
/// 通用公共方法--邮件操作
/// 
/// 版本：Ver 1.0
/// 作者：【周燕龙】
/// 时间：2009-09-08
/// </summary>

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using NotesMail;
using System.Collections;
using System.Security.Cryptography;
using System.IO;
using Hisuntech.SystemFramework.Common;

namespace Hisuntech.SystemFramework.Net
{
    public class Mail
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="subject">主题</param>
        /// <param name="body">内容</param>
        /// <param name="recevieAddress">接收人地址</param>
        /// <param name="recevieName">接收人姓名</param>
        /// <returns>成功返回真，否则返回假</returns>
        public static bool SendMail(string subject, string body, string recevieAddress, string recevieName)
        {
            if (XmlOperate.ReadXml("isnotes", "mail.xml") == "1")
            {
                //发送Notes邮件
                try
                {
                    SendNotesMail mail = new SendNotesMail(Decode(XmlOperate.ReadXml("password", "mail.xml")));
                    mail.Send(recevieAddress, subject, body, null);
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(XmlOperate.ReadXml("username", "mail.xml"), XmlOperate.ReadXml("name", "mail.xml")); //发送方
                msg.To.Add(new MailAddress(recevieAddress, recevieName)); //接收方
                //msg.To.Add(new MailAddress("destination2@domain.com", "Addressee 2's Name")); //发送到多个用户
                msg.Subject = subject; //主题
                //msg.SubjectEncoding = System.Text.Encoding.UTF8;
                msg.Body = body; //内容
                msg.IsBodyHtml = true; //是否允许html
                msg.Priority = MailPriority.High; //优先级
                //msg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                SmtpClient c = new SmtpClient(XmlOperate.ReadXml("smtp", "mail.xml"));
                c.Credentials = new NetworkCredential(XmlOperate.ReadXml("username", "mail.xml"), Decode(XmlOperate.ReadXml("password", "mail.xml")));
                try
                {
                    c.Send(msg);
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="subject">主题</param>
        /// <param name="body">内容</param>
        /// <param name="recevieAddress">接收人地址</param>
        /// <param name="recevieName">接收人姓名</param>
        /// <returns>成功返回真，否则返回假</returns>
        public static bool SendMailGmail(string subject, string body, ArrayList mailAddress)
        {
            if (XmlOperate.ReadXml("isnotes", "mail.xml") == "1")
            {
                //发送Notes邮件
                try
                {
                    SendNotesMail mail = new SendNotesMail(Decode(XmlOperate.ReadXml("password", "mail.xml")));
                    mail.Send((string[])mailAddress.ToArray(), subject, Utils.ClearHTML(body), null);
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(XmlOperate.ReadXml("username", "mail.xml"), XmlOperate.ReadXml("name", "mail.xml")); //发送方

                for (int i = 0; i < mailAddress.Count; i++)
                {
                    msg.To.Add(new MailAddress(mailAddress[i].ToString())); //接收方
                }
                //msg.To.Add(new MailAddress("destination2@domain.com", "Addressee 2's Name")); //发送到多个用户
                msg.Subject = subject; //主题
                //msg.SubjectEncoding = System.Text.Encoding.UTF8;
                msg.Body = body; //内容
                msg.IsBodyHtml = true; //是否允许html
                msg.Priority = MailPriority.High; //优先级
                //msg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                SmtpClient c = new SmtpClient(XmlOperate.ReadXml("smtp", "mail.xml"));
                c.EnableSsl = true;
                c.Credentials = new NetworkCredential(XmlOperate.ReadXml("username", "mail.xml"), Decode(XmlOperate.ReadXml("password", "mail.xml")));
                try
                {
                    c.Send(msg);
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        public static bool SendMail(string subject, string body, ArrayList mailAddress)
        {
            if (XmlOperate.ReadXml("isnotes", "mail.xml") == "1")
            {
                //发送Notes邮件
                try
                {
                    SendNotesMail mail = new SendNotesMail(Decode(XmlOperate.ReadXml("password", "mail.xml")));
                    if (mailAddress.Count == 1)
                        mail.Send(mailAddress[0].ToString(), subject, Utils.ClearHTML(body), null);
                    else
                    {
                        string[] strArr = new String[mailAddress.Count];
                        for (int i = 0; i < mailAddress.Count; i++)
                            strArr[i] = mailAddress[i].ToString();

                        mail.Send(strArr, subject, body, null);
                        //mail.Send((string[])mailAddress.ToArray(), subject, body, null);
                    }
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(XmlOperate.ReadXml("username", "mail.xml"), XmlOperate.ReadXml("name", "mail.xml")); //发送方

                for (int i = 0; i < mailAddress.Count; i++)
                {
                    msg.To.Add(new MailAddress(mailAddress[i].ToString())); //接收方
                }
                //msg.To.Add(new MailAddress("destination2@domain.com", "Addressee 2's Name")); //发送到多个用户
                msg.Subject = subject; //主题
                //msg.SubjectEncoding = System.Text.Encoding.UTF8;
                msg.Body = body; //内容
                msg.IsBodyHtml = true; //是否允许html
                msg.Priority = MailPriority.High; //优先级
                //msg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
                SmtpClient c = new SmtpClient(XmlOperate.ReadXml("smtp", "mail.xml"));

                c.Credentials = new NetworkCredential(XmlOperate.ReadXml("username", "mail.xml"), Decode(XmlOperate.ReadXml("password", "mail.xml")));
                try
                {
                    c.Send(msg);
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }
        const string KEY_64 = "hcljiami";
        const string IV_64 = "HCLJIAMI"; //注意了，是8个字符，64位 
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data">字符串</param>
        /// <returns>解密后的值</returns>
        public static string Decode(string data)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

            byte[] byEnc;
            try
            {
                byEnc = Convert.FromBase64String(data);
            }
            catch
            {
                return null;
            }

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream ms = new MemoryStream(byEnc);
            CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cst);
            return sr.ReadToEnd();
        }




        /// <summary>
        /// 发送带附件的
        /// </summary>
        /// <param name="subject">主题</param>
        /// <param name="body">正文</param>
        /// <param name="recevieAddress">收件人地址</param>
        /// <param name="recevieName"></param>
        /// <param name="filepath">附件路径</param>
        /// <returns></returns>
        public static bool SendMailAsync(string subject, string body, string recevieAddress, string filepath)
        {
            if (XmlOperate.ReadXml("isnotes", "mail.xml") == "1")
            {
                //发送Notes邮件
                try
                {
                    SendNotesMail mail = new SendNotesMail(Decode(XmlOperate.ReadXml("password", "mail.xml")));
                    mail.Send(recevieAddress, subject, body, filepath);
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                try
                {
                    MailMessage mail = new MailMessage(XmlOperate.ReadXml("username", "mail.xml"), recevieAddress);
                    mail.Subject = subject;
                    mail.Body = body;
                    mail.IsBodyHtml = true; //是否允许html
                    mail.Priority = MailPriority.High; //优先级
                    mail.Attachments.Add(new Attachment(filepath));
                    SmtpClient smtp = new SmtpClient(XmlOperate.ReadXml("smtp", "mail.xml"));
                    smtp.Credentials = new NetworkCredential(XmlOperate.ReadXml("username", "mail.xml"), Utils.Decode(XmlOperate.ReadXml("password", "mail.xml")));
                    //smtp.Send(mail);
                    smtp.SendAsync(mail, null);

                }
                catch
                {
                    return false;
                }

            }
            return true;
        }
    }
}
